package me.pinion.listener;

import me.pinion.model.User;
import me.pinion.model.UserInfo;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by shao on 13-12-31.
 */
public class UserSessionListener implements HttpSessionListener{
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        User user = (User) httpSessionEvent.getSession().getAttribute("user");
        if (user != null && user.getId() > 0){
            UserInfo userInfo = user.getUserInfo();
            userInfo.set("last_login", new Timestamp(new Date().getTime())).update();
        }
    }
}
